# -*- coding:utf-8 -*-
import urllib.request
import re
import requests
import sys
from proxy.Manager.ProxyManager import ProxyManager

class MovieTop250:
    def __init__(self):
        self.start = 0
        self.param = '&filter=&type='
        self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64)'}
        self.movieList = []
        self.filePath = 'D:/DoubanTop250.txt'

    def getPage(self):
        try:
            URL = 'http://movie.douban.com/top250?start=' + str(self.start)
            proxy = ProxyManager().get()
            proxies = {"https": "https://{proxy}".format(proxy=proxy)}
            page = requests.get(URL).content.decode("utf-8")
            # page = urllib.request.urlopen("http://movie.douban.com/top250?start=")
            pageNum = (self.start + 25)/25
            print ("正在抓取第{pageNum}页数据".format(pageNum=pageNum))
            self.start += 25
            return page
        except Exception as e:
            if hasattr(e, 'reason'):
                print ("抓取失败，具体原因：{reason}".format(reason=e.reason))

    def getMovie(self):
        pattern = re.compile('<div.*?class="item">.*?<em.*?class="">(.*?)</em>.*?'
                             + '<div.*?class="info">.*?class="title">(.*?)</span>.*?'
                             + 'class="title">&nbsp;/&nbsp;(.*?)</span>.*?'
                             + 'class="other">&nbsp;/&nbsp;(.*?)</span>.*?'
                             + '<div.*?class="bd">.*?<p.*?class="">.*?'
                             + '导演: (.*?)&nbsp;&nbsp;&nbsp;'
                             + '主演: (.*?)<br>(.*?)'
                             + '&nbsp;/&nbsp;(.*?)&nbsp;/&nbsp;(.*?)</p>'
                             + '.*?<div.*?class="star">.*?property="v:average">(.*?)</span>'
                             + '.*?<span>(.*?)人评价</span>.*?<span class="inq">(.*?)</span>.*?</p>')
        while self.start <= 225:
            page = self.getPage()
            movies = re.findall(pattern, page)
            for movie in movies:
                self.movieList.append([movie[0], movie[1], movie[2].lstrip('&nbsp;/&nbsp;'),
                      movie[3].lstrip('&nbsp;/&nbsp;'), movie[4],
                      movie[5], movie[6].lstrip(), movie[7], movie[8].rstrip(),
                      movie[9], movie[10], movie[11]])

    def writeTxt(self):
        fileTop250 = open(self.filePath, 'w')
        try:
            for movie in self.movieList:
                fileTop250.write('电影排名：' + movie[0] + '\r\n')
                fileTop250.write('电影名称：' + movie[1] + '\r\n')
                fileTop250.write('外文名称：' + movie[2] + '\r\n')
                fileTop250.write('电影别名：' + movie[3] + '\r\n')
                fileTop250.write('导演姓名：' + movie[4] + '\r\n')
                fileTop250.write('参与主演：' + movie[5] + '\r\n')
                fileTop250.write('上映年份：' + movie[6] + '\r\n')
                fileTop250.write('制作国家/地区：' + movie[7] + '\r\n')
                fileTop250.write('电影类别：' + movie[8] + '\r\n')
                fileTop250.write('电影评分：' + movie[9] + '\r\n')
                fileTop250.write('参评人数：' + movie[10] + '\r\n')
                fileTop250.write('简短影评：' + movie[11] + '\r\n\r\n')
            print ("文件写入成功")
        except Exception as e:
            print(e)
        finally:
            fileTop250.close()

    def main(self):
        print ("正在从豆瓣电影Top250抓取数据")
        self.getMovie()
        self.writeTxt()
        print ("抓取完毕")

DouBanSpider = MovieTop250()
DouBanSpider.main()